$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;

    //搜索框 获取焦点
    $("#goods_id").focus();

    //点击返回箭头 清除 缓存 打开 storekeeper.html
    $(".head .goBack").on("click", function () {
        window.location.href = "storekeeper.html";
    });

    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            $("#goods_id").val("");
            if (!val) {
                $alert("请扫描原料标签！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            } else {
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/unbonded/getUnbondedMaterial',
                    data: {
                        batch: val
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        pass = 1;
                        loading_hide();
                        if (res.success) {
                            var data = res.result;
                            $alert(val + "扫描成功");
                            //填充 当前机台与当前用料
                            showplandata(res);
                        } else {
                            console.log(res);
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        pass = 1;
                        loading_hide();
                        console.log(error);
                        $alert(error.responseText);
                    }
                });
            }
        }
    });

    function showplandata(res) {
        var data = res.result;
        $("section span.work_batch").html(data.batch);
        $("section span.work_name").html(data.inventoryName);
        $("section span.work_gg").html(data.inventorySpecification);
        $("section span.work_zl").html(data.availableQuantity);
    }

    //点击 完工
    $(".bnt .workover").click(function () {
        var batch = $("section span.work_batch").html();
        if (!batch || batch == "空") {
            $alert("请先扫描原料");
        }
        $.confirm("确认无粘结加工完成吗？", function () {
            if (pass == 0) return;
            pass = 0;
            loading_show("提交中...");
            //层卷 收线
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/unbonded/unbondedComplete',
                data: {
                    batch: batch
                },
                timeout: 30000,
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    if (res.success) {
                        pass = 1;
                        $alert("加工成功！");
                        setTimeout(function () {
                            window.location.href = "unbonded.html";
                        }, 1200);
                        if (res.result) {
                            takeupPrint("unbondedTakeup", res.result);
                        }
                    } else {
                        pass = 1;
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    if (error == 0) {
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        console.log(error);
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        });
    });

});
